<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .father{
            width: 280px;
            height: 400px;
            background: orange;
        }

        .son{
            width: 280px;
            height: 200px;
            margin-top: 100px;
            background: skyblue;
        }
    </style>
</head>

<body>
    <div id="app">
        <h3>子向父2</h3>
        <father/>
    </div>



    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <!-- 父组件 -->
    <template id="father">
        <div class="father">
            <h2>我是父组件 --- {{ val }}</h2>
            <son @send="fn" />
        </div>
    </template>



    <!-- 子组件 -->
    <template id="son">
        <div class="son">
            <h2>我是子组件---{{ num }}</h2>
            <button @click="sendNum">发送数据</button>
        </div>
    </template>

    <script>

        // 子组件
        const Son = {
            template: "#son",
            data() {
                return {
                    num: 8888
                }
            },
            methods: {
                sendNum() {
                    // $emit('自定义事件名称', 要发过去的数据)
                    this.$emit('send', this.num)
                }
            }
        }

        // 父组件
        const Father = {
            template: "#father",
            components: {
                'son': Son
            },
            data() {
                return {
                    val: ""
                }
            },
            methods: {
                fn(val) {
                    this.val = val
                }
            }
        }

        

        const app = new Vue({
            el: "#app",
            components: {
                'father': Father
            }
        })

        // 子向父传递数据必须通过事件来触发
        // 
    </script>
</body>

</html>